package com.motus.pm.daoImpl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.motus.pm.dao.ProjectTaskDao;
import com.motus.pm.vo.PmProjectTask;

@Repository
public class ProjectTaskDaoImpl extends BaseDaoImpl<PmProjectTask> implements ProjectTaskDao {

	@Autowired
	private SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked")
	@Override
	public List<PmProjectTask> tasksByState(int state,int userId) {
		String hql = "from PmProjectTask   where taskState =? and motusEmpInfoByUserId.id =? order by taskPlanEndTime asc"; 
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql);
		query.setInteger(0, state);
		query.setInteger(1, userId);
		return query.list();
	}

	@Override
	public PmProjectTask taskById(int taskId) {
		return super.getById(taskId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PmProjectTask> getProjectTasksByDeptIdAndTaskState(int deptId,
			int taskState) {
//		String hql="from PmProjectTask where taskState=?"+
//				    "and motusEmpInfoByUserId.motusDept.deptid =?";
//		Session session = sessionFactory.getCurrentSession();
//		Query query = session.createQuery(hql);
		String sql="select * from pm_project_task where task_state = ? and user_id in (select id from motus_emp_info where deptid=?)";
		Session session = sessionFactory.getCurrentSession();
		Query query=session.createSQLQuery(sql).addEntity(PmProjectTask.class);
		query.setInteger(0, taskState);
		query.setInteger(1, deptId);
		return query.list();
	}
}
